﻿Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading


Public Class Form1
    '100mb文件传输实现方案
    Public listner As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
    Public T As Thread
    Public IPEndPoin As IPEndPoint
    Public strSavePath As String

    Public Function MB_TO_Bytes(ByVal MB As Integer) As Integer
        Dim intBytes As Integer
        intBytes = MB * 1024 * 1024
        Return intBytes
    End Function

    Private Sub Listen()
        Try
            Dim bytes(MB_TO_Bytes(NumericUpDown1.Value)) As Byte
            Dim NumGet As Integer
            While True
                NumGet = listner.Receive(bytes)
                Dim recfs As New FileStream(strSavePath, FileMode.OpenOrCreate)
                Dim newfilestr As New BinaryWriter(recfs)
                newfilestr.Write(bytes, 0, NumGet)
                recfs.Close()
                ListBox1.Items.Add("接受数据成功。共计：" & NumGet)
            End While
        Catch ex As Exception
            ListBox1.Items.Add(ex.ToString)
        End Try
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            IPEndPoin = New Net.IPEndPoint(Net.IPAddress.Parse(Trim(TextBox1.Text)), CInt(TextBox2.Text))
            listner.Bind(IPEndPoin)
            T = New Thread(AddressOf Listen)
            T.Start()
            ListBox1.Items.Add("成功启动监听")
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
